<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Yaf_Controller_Abstract class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="yaf-config-simple.valid.html">« Yaf_Config_Simple::valid</a></li>
      <li style="float: right;"><a href="yaf-controller-abstract.construct.html">Yaf_Controller_Abstract::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.yaf.html">Yaf</a></li>
    <li>The Yaf_Controller_Abstract class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.yaf-controller-abstract" class="reference">

 <h1 class="title">The Yaf_Controller_Abstract class</h1>
 

 <div class="partintro"><p class="verinfo">(Yaf &gt;=1.0.0)</p>


  <div class="section" id="yaf-controller-abstract.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    <span class="classname"><strong class="classname">Yaf_Controller_Abstract</strong></span> 是Yaf的MVC体系的核心部分。
    MVC是指Model-View-Controller, 是一个用于分离应用逻辑和表现逻辑的设计模式。
   </p>
   <p class="para">
    每个用户自定义controller都应当继承<span class="classname"><strong class="classname">Yaf_Controller_Abstract</strong></span>。
   </p>
   <p class="para">
    你会发现在你自己的controller中无法定义__construct方法。因此，<span class="classname"><strong class="classname">Yaf_Controller_Abstract</strong></span>
    提供了一个魔术方法Yaf_Controller_Abstract::init()。
   </p>
   <p class="para">
    如果在你自己的controller里面已经定义了一个init()方法，当你的controller被实例化的时候，它将被调用。
   </p>
   <p class="para">
    Action可能需要参数，当一个请求来到的时候，在路由中如果请求的参数有相同名称的变量（例如：<span class="methodname"><a href="yaf-request-abstract.getparam.html" class="methodname">Yaf_Request_Abstract::getParam()</a></span>），
    Yaf将把他们传递给action方法（see <span class="methodname"><a href="yaf-action-abstract.execute.html" class="methodname">Yaf_Action_Abstract::execute()</a></span>）。
   </p>
  </div>


  <div class="section" id="yaf-controller-abstract.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">abstract</span>
      <span class="modifier">class</span> <strong class="classname">Yaf_Controller_Abstract</strong>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 属性 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">public</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.actions">$<var class="varname">actions</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.module">$<var class="varname">_module</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.name">$<var class="varname">_name</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.request">$<var class="varname">_request</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.response">$<var class="varname">_response</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.invoke-args">$<var class="varname">_invoke_args</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">protected</span>
      <var class="varname"><a href="class.yaf-controller-abstract.html#yaf-controller-abstract.props.view">$<var class="varname">_view</var></a></var>;</div>


    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">private</span> <span class="methodname"><a href="yaf-controller-abstract.construct.html" class="methodname">__construct</a></span>()</div>
<div class="methodsynopsis dc-description"><span class="modifier">protected</span> <span class="methodname"><a href="yaf-controller-abstract.display.html" class="methodname">display</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$tpl</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$parameters</code><span class="initializer"> = ?</span></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.forward.html" class="methodname">forward</a></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$module</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$controller</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$action</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">array</span> <code class="parameter">$paramters</code><span class="initializer"> = ?</span></span><br>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getinvokearg.html" class="methodname">getInvokeArg</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getinvokeargs.html" class="methodname">getInvokeArgs</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getmodulename.html" class="methodname">getModuleName</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getname.html" class="methodname">getName</a></span>(): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getrequest.html" class="methodname">getRequest</a></span>(): <span class="type"><a href="class.yaf-request-abstract.html" class="type Yaf_Request_Abstract">Yaf_Request_Abstract</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getresponse.html" class="methodname">getResponse</a></span>(): <span class="type"><a href="class.yaf-response-abstract.html" class="type Yaf_Response_Abstract">Yaf_Response_Abstract</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getview.html" class="methodname">getView</a></span>(): <span class="type"><a href="class.yaf-view-interface.html" class="type Yaf_View_Interface">Yaf_View_Interface</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.getviewpath.html" class="methodname">getViewpath</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.init.html" class="methodname">init</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.initview.html" class="methodname">initView</a></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.redirect.html" class="methodname">redirect</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$url</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">protected</span> <span class="methodname"><a href="yaf-controller-abstract.render.html" class="methodname">render</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$tpl</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$parameters</code><span class="initializer"> = ?</span></span>): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="yaf-controller-abstract.setviewpath.html" class="methodname">setViewpath</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$view_directory</code></span>): <span class="type"><span class="type void">void</span></span></div>

   }</div>


  </div>

  

  <div class="section" id="yaf-controller-abstract.props">
   <h2 class="title">属性</h2>
   <dl>
    
     <dt id="yaf-controller-abstract.props.actions"><var class="varname">actions</var></dt>

     <dd>

      <p class="para">
      你也可以通过使用这个值和 <span class="classname"><a href="class.yaf-action-abstract.html" class="classname">Yaf_Action_Abstract</a></span> 在一个单独的PHP脚本中定义action函数。
      <div class="example" id="example-3889">
       <p><strong>示例 #1 在一个独立的文件中定义action</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IndexController&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Yaf_Controller_Abstract&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$actions&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**&nbsp;now&nbsp;dummyAction&nbsp;is&nbsp;defined&nbsp;in&nbsp;a&nbsp;separate&nbsp;file&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"dummy"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"actions/Dummy_action.php"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;action&nbsp;method&nbsp;may&nbsp;have&nbsp;arguments&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">indexAction</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getRequest</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getParam</span><span style="color: #007700">(</span><span style="color: #DD0000">"name"</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$id&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_request</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getParam</span><span style="color: #007700">(</span><span style="color: #DD0000">"id"</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

      </div>
      <div class="example" id="example-3890">
       <p><strong>示例 #2 Dummy_action.php</strong></p>
        <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">DummyAction&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Yaf_Action_Abstract&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;a&nbsp;action&nbsp;class&nbsp;shall&nbsp;define&nbsp;this&nbsp;method&nbsp;&nbsp;as&nbsp;the&nbsp;entry&nbsp;point&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">execute</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

      </div>
      </p>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.module"><var class="varname">_module</var></dt>

     <dd>

      <p class="para">
        模块名
      </p>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.name"><var class="varname">_name</var></dt>

     <dd>

      <p class="para"/>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.request"><var class="varname">_request</var></dt>

     <dd>

      <p class="para">
      当前的请求实例
      </p>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.response"><var class="varname">_response</var></dt>

     <dd>

      <p class="para"/>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.invoke-args"><var class="varname">_invoke_args</var></dt>

     <dd>

      <p class="para"/>
     </dd>

    
    
     <dt id="yaf-controller-abstract.props.view"><var class="varname">_view</var></dt>

     <dd>

      <p class="para">
      视图引擎
      </p>
     </dd>

    
   </dl>

  </div>



 </div>

 



















































































































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="yaf-controller-abstract.construct.html">Yaf_Controller_Abstract::__construct</a> — Yaf_Controller_Abstract constructor</li><li><a href="yaf-controller-abstract.display.html">Yaf_Controller_Abstract::display</a> — The display purpose</li><li><a href="yaf-controller-abstract.forward.html">Yaf_Controller_Abstract::forward</a> — The forward purpose</li><li><a href="yaf-controller-abstract.getinvokearg.html">Yaf_Controller_Abstract::getInvokeArg</a> — The getInvokeArg purpose</li><li><a href="yaf-controller-abstract.getinvokeargs.html">Yaf_Controller_Abstract::getInvokeArgs</a> — The getInvokeArgs purpose</li><li><a href="yaf-controller-abstract.getmodulename.html">Yaf_Controller_Abstract::getModuleName</a> — 获取当前控制器所属的模块名</li><li><a href="yaf-controller-abstract.getname.html">Yaf_Controller_Abstract::getName</a> — Get self name</li><li><a href="yaf-controller-abstract.getrequest.html">Yaf_Controller_Abstract::getRequest</a> — The getRequest purpose</li><li><a href="yaf-controller-abstract.getresponse.html">Yaf_Controller_Abstract::getResponse</a> — The getResponse purpose</li><li><a href="yaf-controller-abstract.getview.html">Yaf_Controller_Abstract::getView</a> — 获取当前的视图引擎</li><li><a href="yaf-controller-abstract.getviewpath.html">Yaf_Controller_Abstract::getViewpath</a> — The getViewpath purpose</li><li><a href="yaf-controller-abstract.init.html">Yaf_Controller_Abstract::init</a> — 控制器初始化</li><li><a href="yaf-controller-abstract.initview.html">Yaf_Controller_Abstract::initView</a> — The initView purpose</li><li><a href="yaf-controller-abstract.redirect.html">Yaf_Controller_Abstract::redirect</a> — The redirect purpose</li><li><a href="yaf-controller-abstract.render.html">Yaf_Controller_Abstract::render</a> — 渲染视图模板</li><li><a href="yaf-controller-abstract.setviewpath.html">Yaf_Controller_Abstract::setViewpath</a> — The setViewpath purpose</li></ul>
</div>
</div></div></body></html>